<?php
/**
 ***********************************************************************************************************************
 * 快递员账号服务
 ***********************************************************************************************************************
 */
namespace soa\account;
use soa\client;
use artisan\cache;

/**
 * Class courier
 */
class courier extends client
{
    /**
     * @var string
     */
    protected $service_url = 'http://account.core.kuaidihelp.com/';

    /**
     * @var string
     */
    protected $service_name = 'kuaidihelp_account';

    /**
     * 根据快递员id获取快递员信息
     *
     * @param $courier_id
     * @return array|mixed
     */
    public function getById($courier_id)
    {
        if(empty($courier_id) || !is_numeric($courier_id)) {
            return false;
        }
        return $this->get('v1/courier/info', ['courier_id' => $courier_id]) ?: false;
    }

    /**
     * 根据快递员id获取快递员信息
     *
     * @param $courier_id
     * @return array
     */
    public function getCourierById($courier_id)
    {
        return $this->getById($courier_id);
    }

    /**
     * 根据快递员ID从缓存里获取快递员信息
     */
    public function getCourierByIdFromCache($courier_id){
        $cacheKey = 'cache_courier_info_comm_';
        $courierInfo = cache::connect('info')->get($cacheKey.$courier_id);
        if(empty($courierInfo)){
            $objInfo = $this->getCourierById($courier_id);
            if(!empty($objInfo)){
                $data = [
                    'username' => $objInfo['username'],
                    'realname' => $objInfo['realname'],
                    'index_shop_id' => $objInfo['index_shop_id'],
                    'index_shop_name' => $objInfo['index_shop_name'],
                    'brand' => $objInfo['brand'],
                    'county'=> $objInfo['county']
                ];
                cache::connect('info')->set($cacheKey.$courier_id, json_encode($data), 259200); //缓存3天
            }else{
                $data = [
                    'username' => '',
                    'realname' => '',
                    'index_shop_id' => 0,
                    'index_shop_name' => '',
                    'brand' => '',
                    'county'=> ''
                ];
            }
            unset($objInfo);
            return $data;
        }else{
            //logDebug($courierInfo, 'test_getcminfo_from_cache');
            return json_decode($courierInfo, TRUE);
        }
    }

    /**
     * 根据快递员手机号码获取快递员信息
     *
     * @param $mobile
     * @return bool|null
     */
    public function getByMobile($mobile)
    {
        if(empty($mobile) || !is_numeric($mobile) || strlen($mobile) != 11) {
            return false;
        }
        return $this->get('v1/courier/info', array('username' => $mobile)) ?: false;
    }

    /**
     * 根据快递员手机号码获取快递员信息
     *
     * @param $mobile
     * @return bool|null
     */
    public function getCourierByMobile($mobile)
    {
        return $this->getByMobile($mobile);
    }

    /**
     * 根据快递员手机号码获取快递员信息
     *
     * @param $mobile
     * @return array|bool
     */
    public function getByName($mobile)
    {
        return $this->getByMobile($mobile);
    }

    /**
     * 根据快递员手机号码获取快递员信息
     *
     * @param $mobile
     * @return array|bool
     */
    public function getCourierByName($mobile)
    {
        return $this->getByMobile($mobile);
    }

    /**
     * 添加快递员 网点（快宝）后台特定功能 username\brand\index_shop_id\index_shop_name 不能为空
     *
     * @param array $data
     * @return int
     */
    public function add(array $data)
    {
        if(empty($data) || !is_array($data)) {
            return false;
        }
        return $this->post('v1/courier/add', $data) ?: 0;
    }

    /**
     * 编辑单个快递员信息
     *
     * @param $courier_id
     * @param array $data
     * @return bool
     */
    public function update($courier_id, array $data)
    {
        if(empty($courier_id) || !is_numeric($courier_id) || empty($data) || !is_array($data)) {
            return false;
        }
        return $this->post('v1/courier/update', array_merge(array('courier_id' => $courier_id), $data)) ? true : false;
    }

    /**
     * 设置快递员认证状态
     *
     * @param $courier_id
     * @param $auth
     */
     public function verify($courier_id, $auth){
         if(empty($courier_id)) {
             return false;
         }
         return $this->post('v1/courier/verify', ['courier_id' => $courier_id,'auth'=>$auth]) ? true : false;
     }


    /**
     * 逻辑删除一个快递员
     *
     * @param $courier_id
     * @param array $data
     * @return bool
     */
    public function del($courier_id)
    {
        if(empty($courier_id) || !is_numeric($courier_id)) {
            return false;
        }
        return $this->post('v1/courier/update', ['courier_id' => $courier_id, 'is_deleted' => 1]) ? true : false;
    }


    /**
     * 批量获取快递员数据列表
     *
     * @param array $where
     * @param array $limit
     * @param string $order_by
     * @return bool
     */
    public function getCouriers(array $where = [], $page = 1, $page_num = 30, $order_by = '')
    {
        $data = array(
            'page' => $page,
            'page_num' => $page_num,
            'order_by' => $order_by
        );

        // 支持in查询的字段\支持bettween查询的
        $in_fields = array(
            'index_shop_id' => 'index_shop_id',
            'id in' => 'ids',
            'brand in' => 'brands',
            'username in' => 'usernames',
            'create_at >=' => 'create_at_start',
            'create_at <' => 'create_at_end',
            'online_time >=' => 'online_time_start',
            'online_time <' => 'online_time_end',
            'register_acive_time >=' => 'register_acive_time_start',
            'register_acive_time <' => 'register_acive_time_end',
            'time >=' => 'time_start',
            'time <' => 'time_end',
            'last_login_time >=' => 'last_login_time_start',
            'last_login_time <' => 'last_login_time_end',
        );

        // 组织条件
        foreach((array)$where as $key => $val) {
            if(in_array($key, array_keys($data))) {
                continue;
            }
            $key = preg_replace('/\s+/', ' ', trim($key));

            if(in_array($key, array_keys($in_fields))) {
                $data[$in_fields[$key]] = $val;
            }
        }

        return $this->post('v1/courier/find', $data);
    }


    /**
     * 根据多个快递员手机号批量获取快递员
     *
     * @param array $mobiles
     * @return array|bool
     */
    public function getCouriersByMobiles(array $mobiles)
    {
        if(empty($mobiles) || empty(array_filter($mobiles))) {
            return array();
        }
        return $this->post('v1/courier/find', array('usernames' => implode(',', $mobiles), 'page_num' => count($mobiles)));
    }


    /**
     * 根据多个快递员ids批量获取快递员
     * @param array $mobiles
     * @return array|bool
     */
    public function getCouriersByIds(array $courier_ids)
    {
        if(empty($courier_ids) || empty(array_filter($courier_ids))) {
            return array();
        }
        return $this->post('v1/courier/find', array('ids' => implode(',', $courier_ids), 'page_num' => count($courier_ids)));
    }

    /**
     * @param $courier_phone
     * @return bool
     */
    public function getIsblack($courier_phone, $courier_id = 0)
    {
      if(empty($courier_phone) && empty($courier_id)) {
           return false;
      }
      return $this->post('v1/courier/blacklist', array('courier_phone' => $courier_phone, 'courier_id' => $courier_id));
    }

    public function intoBlack($data){
        return $this->post('v1/courier/blacklist/intoBlack', array('cm_id' => $data['cm_id'], 'cm_phone' => $data['cm_phone'], 'note' => $data['note']));
    }

    public function isScanBinding($data){
        return $this->post('v1/courier/scaner/chkScanBinding', array('cm_phone' => $data['cm_phone'], 'brand' => $data['brand']));
    }

    public function getVipInfo($data){
        return $this->post('v1/courier/vip/getVipInfo', array('cm_id' => $data['cm_id']));
    }


    /**
     * 业务员手机号码转编号
     * @param $mobile
     * @param string $brand
     * @return string
     */
    public function mobile2Code($mobile,$brand = "sto"){
        if(empty($mobile)){return "";}
        $courierInfo = $this->post('v1/courier/Courier/mobile2Code', array("mobile" => $mobile,"brand" => $brand));
        if($courierInfo["cm_code"]){return $courierInfo["cm_code"];}
        return "";
    }

/**
 ***********************************************************************************************************************
 * 新版账号系统提供统一服务列表
 ***********************************************************************************************************************
 */

    /**
     * 根据session_id获取当前快递员会话信息
     *
     * @param string $session_id
     * @return array|bool
     */
    public function getSession($session_id)
    {
        return $this->post('v1/courier/session', array('session_id' => $session_id));
    }


    /**
     * 每天超过5次密码失败 将会被锁定账号一天
     *
     * @param $username
     * @param $password
     * @return array
     */
    public function signin($username, $password)
    {
        if(empty($username) || !is_string($username) || empty($password) || !is_string($password)) {
            return false;
        }
        return $this->post('v1/courier/signin', ['username' => $username, 'password' => $password]);
    }

    /**
     * 注册快递员账号
     *
     * 不传smscode会发送验证码
     * @param array $dict
     * @return array
     */
    public function signup(array $dict)
    {
        return $this->post('v1/courier/signup', $dict);
    }

    /**
     * 重置快递员密码 该操作会删除当前手机号名下的所有设备的session会话信息
     *
     * 不传smscode会发送验证码
     * @param $password
     */
    public function resetPwd($username, $smscode = '', $pwd = '')
    {
        return $this->post('v1/courier/reset/pwd', ['username' => $username, 'password' => $pwd, 'smscode' => $smscode]);
    }

    /**
     * 修改资料
     *
     * @param array $dict
     * @return bool
     */
    public function modify(array $dict)
    {
        return $this->post('v1/courier/modify', $dict);
    }

}